package cn.yuxiaoyuan.article.controller;

import cn.yuxiaoyuan.annotation.PreAuthorize;
import cn.yuxiaoyuan.article.domain.Article;
import cn.yuxiaoyuan.article.domain.ArticleType;
import cn.yuxiaoyuan.article.query.ArticleQuery;
import cn.yuxiaoyuan.article.query.ArticleTypeQuery;
import cn.yuxiaoyuan.article.service.ArticleService;
import cn.yuxiaoyuan.article.service.ArticleTypeService;
import cn.yuxiaoyuan.constant.RedisConstant;
import cn.yuxiaoyuan.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Api("Article相关的接口")
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PreAuthorize(sn = "article:findById",name = "根据id查询文章",descs = "根据id查询文章")
    @GetMapping("/{id}")
    public R findById(@PathVariable("id")Long id){
        return R.ok(articleService.findById(id));
    }

    @PreAuthorize(sn = "article:findAll",name = "查询所有文章",descs = "查询所有文章")
    @GetMapping
    public R findAll(){
        return R.ok(articleService.findAll());
    }

    @PreAuthorize(sn = "article:deleteById",name = "根据id删除文章",descs = "根据id删除文章")
    @DeleteMapping("/{id}")
    public R deleteById(@PathVariable("id")Long id){
        articleService.deleteById(id);
        return R.ok();
    }

    @PreAuthorize(sn = "article:deleteAll",name = "根据ids删除文章",descs = "根据ids删除文章")
    @PostMapping("/deleteAll")
    public R batchDelete(@RequestBody List<Long> ids){
        articleService.batchDelete(ids);
        return R.ok();
    }

    @PreAuthorize(sn = "article:star",name = "点赞",descs = "点赞")
    @GetMapping("/star/{id}")
    public R star(HttpServletRequest request,@PathVariable("id")Long id){
        Integer star = articleService.star(request, id);
        return R.ok(star);
    }

    @PreAuthorize(sn = "article:add",name = "添加和修改文章",descs = "添加和修改文章")
    @ApiOperation("添加和修改")
    @PostMapping
    public R add(@RequestBody Article article){
        if(article.getId() == null){
            article.setCreateTime(new Date());
            articleService.add(article);
        }else {
            article.setUpdateTime(new Date());
            articleService.update(article);
        }
        return R.ok();
    }
    @PreAuthorize(sn = "article:getPage",name = "文章的分页查询",descs = "文章的分页查询")
    @PostMapping("/page")
    public R getPage(@RequestBody ArticleQuery query){
        PageHelper.startPage(query.getPage(),query.getSize());
        List<Article> articles = articleService.queryPage(query);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return R.ok(pageInfo);
    }

    @PreAuthorize(sn = "article:selectPage",name = "用户端的分页查询",descs = "用户端的分页查询")
    @PostMapping("/selectPage")
    public R selectPage(@RequestBody ArticleQuery query){
        PageHelper.startPage(query.getPage(),query.getSize());
        List<Article> articles = articleService.selectPage(query);
        PageInfo<Article> pageInfo = new PageInfo<>(articles);
        return R.ok(pageInfo);
    }

    @PreAuthorize(sn = "article:articleTypeCount",name = "文章类型计数",descs = "文章类型计数")
    @GetMapping("/articleTypeCount")
    public R articleTypeCount(){
        return R.ok(articleService.articleTypeCount());
    }

    @PreAuthorize(sn = "article:archives",name = "文章归档",descs = "文章归档")
    @GetMapping("/archives")
    public R archives(){
     return R.ok(articleService.archives());
    }
}
